<template>
  <div class="app-container">
    <el-card class="box-card">
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true"  label-width="68px">
<!--      <el-form-item label="用户ID" prop="userId">
        <el-input
          v-model="queryParams.userId"
          placeholder="请输入用户ID"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="部门ID" prop="deptId">
        <el-input
          v-model="queryParams.deptId"
          placeholder="请输入部门ID"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>-->
<!--      <el-form-item label="用户昵称" prop="nickName">
        <el-input
          v-model="queryParams.nickName"
          placeholder="请输入用户昵称"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>-->
      <el-form-item label="投票标题" prop="voteTitile">
        <el-input
          v-model="queryParams.voteTitile"
          placeholder="请输入投票标题"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
<!--      <el-form-item label="结束时间" prop="endTime">
        <el-input
          v-model="queryParams.endTime"
          placeholder="请输入结束时间"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>-->
<!--      <el-form-item label="是否匿名投票" prop="anonoymity">
        <el-input
          v-model="queryParams.anonoymity"
          placeholder="请输入是否匿名投票"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>-->
<!--      <el-form-item label="投票结果" prop="voteResult">
        <el-input
          v-model="queryParams.voteResult"
          placeholder="请输入投票结果"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="个人投票状态" prop="personalVoteState">
        <el-input
          v-model="queryParams.personalVoteState"
          placeholder="请输入个人投票状态"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>-->
<!--      <el-form-item label="投票状态" prop="voteState">
        <el-input
          v-model="queryParams.voteState"
          placeholder="请输入投票状态"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>-->
<!--      <el-form-item label="备注1" prop="note1">
        <el-input
          v-model="queryParams.note1"
          placeholder="请输入备注1"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="备注2" prop="note2">
        <el-input
          v-model="queryParams.note2"
          placeholder="请输入备注2"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="备注3" prop="note3">
        <el-input
          v-model="queryParams.note3"
          placeholder="请输入备注3"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="备注4" prop="note4">
        <el-input
          v-model="queryParams.note4"
          placeholder="请输入备注4"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="备注5" prop="note5">
        <el-input
          v-model="queryParams.note5"
          placeholder="请输入备注5"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>-->
      <br/>
      <div style="text-align: right;">
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </div>
      <br/>
    </el-form>

    <el-row :gutter="10" class="mb8">
<!--      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
          v-hasPermi="['dr:vote:add']"
        >创建投票</el-button>
      </el-col>-->
<!--      <el-col :span="1.5">
        <el-button
          type="success"
          plain
          icon="el-icon-edit"
          size="mini"
          :disabled="single"
          @click="handleUpdate"
          v-hasPermi="['dr:vote:edit']"
        >修改</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="danger"
          plain
          icon="el-icon-delete"
          size="mini"
          :disabled="multiple"
          @click="handleDelete"
          v-hasPermi="['dr:vote:remove']"
        >删除</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="el-icon-download"
          size="mini"
          @click="handleExport"
          v-hasPermi="['dr:vote:export']"
        >导出</el-button>
      </el-col>-->
<!--     <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>-->
    </el-row>

    <el-table v-loading="loading"border :data="voteList" @selection-change="handleSelectionChange">
<!--      <el-table-column type="selection" width="55" align="center" />-->
<!--     <el-table-column label="ID" align="center" prop="id" />-->
      <el-table-column type="index" label="序号"   :index="indexMethod" align="center"/>
      <!--  <el-table-column label="用户ID" align="center" prop="userId" />
       <el-table-column label="部门ID" align="center" prop="deptId" />-->
      <el-table-column label="用户昵称" align="center" prop="nickName" />
      <el-table-column label="投票标题" align="center" prop="voteTitile" />
      <el-table-column label="投票类型" align="center" prop="voteType" >
        <template slot-scope="scope">
          <span v-if="scope.row.voteType==1">单选</span>
          <span v-if="scope.row.voteType==2">多选</span>
        </template>
      </el-table-column>
      <el-table-column label="结束时间" align="center" prop="endTime" />
<!--      <el-table-column label="是否匿名投票" align="center" prop="anonoymity" />
      <el-table-column label="投票结果" align="center" prop="voteResult" />-->
<!--      <el-table-column label="个人投票状态" align="center" prop="personalVoteState" />-->
      <el-table-column label="投票状态" align="center" prop="voteState">
        <template slot-scope="scope">
          <span v-if="scope.row.voteState==1">未发布</span>
          <span v-if="scope.row.voteState==2">进行中</span>
          <span v-if="scope.row.voteState==3">已截止</span>
        </template>
      </el-table-column>
<!--      <el-table-column label="备注1" align="center" prop="note1" />
      <el-table-column label="备注2" align="center" prop="note2" />
      <el-table-column label="备注3" align="center" prop="note3" />
      <el-table-column label="备注4" align="center" prop="note4" />
      <el-table-column label="备注5" align="center" prop="note5" />-->
      <el-table-column label="操作"fixed="right" align="center" class-name="small-padding fixed-width">

        <template slot-scope="scope">
<!--          <el-button
            size="mini"
            type="text"
            @click="handleUpdate(scope.row)"
            v-hasPermi="['dr:vote:edit']"
          >修改</el-button>
          <el-button
            size="mini"
            type="text"
            @click="handleDelete(scope.row)"
            v-hasPermi="['dr:vote:remove']"
          >删除</el-button>-->
          <el-button
            @click="addQuestion(scope.row)"
            type="text"
            size="small"
          >进入投票</el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />
    </el-card>
    <!-- 添加或修改投票信息对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="1000px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
<!--        <el-form-item label="用户ID" prop="userId">
          <el-input v-model="form.userId" placeholder="请输入用户ID" />
        </el-form-item>
        <el-form-item label="部门ID" prop="deptId">
          <el-input v-model="form.deptId" placeholder="请输入部门ID" />
        </el-form-item>-->
<!--        <el-form-item label="用户昵称" prop="nickName">
          <el-input v-model="form.nickName" placeholder="请输入用户昵称" />
        </el-form-item>-->
        <el-form-item label="投票标题" prop="voteTitile">
          <el-input v-model="form.voteTitile" placeholder="请输入投票标题" />
        </el-form-item>
        <el-form-item label="投票类型" prop="voteTitile">
          <el-input v-model="form.voteType" placeholder="请输入投票类型" />
        </el-form-item>
        <el-form-item label="结束时间" prop="endTime">
          <el-input v-model="form.endTime" placeholder="请输入结束时间" />
        </el-form-item>
<!--        <el-form-item label="是否匿名投票" prop="anonoymity">
          <el-input v-model="form.anonoymity" placeholder="请输入是否匿名投票" />
        </el-form-item>-->
<!--        <el-form-item label="投票结果" prop="voteResult">
          <el-input v-model="form.voteResult" placeholder="请输入投票结果" />
        </el-form-item>-->
<!--        <el-form-item label="个人投票状态" prop="personalVoteState">
          <el-input v-model="form.personalVoteState" placeholder="请输入个人投票状态" />
        </el-form-item>
        <el-form-item label="投票状态" prop="voteState">
          <el-input v-model="form.voteState" placeholder="请输入投票状态" />
        </el-form-item>
        <el-form-item label="备注" prop="note">
          <el-input v-model="form.note" type="textarea" placeholder="请输入内容" />
        </el-form-item>
        <el-form-item label="备注1" prop="note1">
          <el-input v-model="form.note1" placeholder="请输入备注1" />
        </el-form-item>
        <el-form-item label="备注2" prop="note2">
          <el-input v-model="form.note2" placeholder="请输入备注2" />
        </el-form-item>
        <el-form-item label="备注3" prop="note3">
          <el-input v-model="form.note3" placeholder="请输入备注3" />
        </el-form-item>
        <el-form-item label="备注4" prop="note4">
          <el-input v-model="form.note4" placeholder="请输入备注4" />
        </el-form-item>
        <el-form-item label="备注5" prop="note5">
          <el-input v-model="form.note5" placeholder="请输入备注5" />
        </el-form-item>-->
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { listVote, getVote, delVote, addVote, updateVote } from "@/api/module/dr/vote";
import watermark from "watermark-dom";
export default {
  name: "Vote",
  data() {
    return {
      mounted() {
        watermark.load({ watermark_txt: "  "   });
      },
      //携带当前行的参数数组
      carryCurrentRowCode: {
        nickName: "",
        voteTitile: "",
        voteType: "",
        id: "",
        voteState:""

      },
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 投票信息表格数据
      voteList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        userId: null,
        deptId: null,
        nickName: null,
        voteTitile: null,
        voteType: null,
        endTime: null,
        anonoymity: null,
        voteResult: null,
        personalVoteState: null,
        voteState: 2,
        note: null,
        note1: null,
        note2: null,
        note3: null,
        note4: null,
        note5: null
      },
      // 表单参数
      form: {
      },
      // 表单校验
      rules: {
      }
    };
  },
  created() {
    this.getList();
  },
  methods: {
    indexMethod(index) {
      index = (index + 1) + (this.queryParams.pageNum - 1) * this.queryParams.pageSize
      return index
    },
    /** 查询投票信息列表 */
    getList() {
      this.loading = true;
      listVote(this.queryParams).then(response => {
        this.voteList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        id: null,
        userId: null,
        deptId: null,
        nickName: null,
        voteTitile: null,
        voteType: null,
        endTime: null,
        anonoymity: null,
        voteResult: null,
        personalVoteState: null,
        voteState: null,
        note: null,
        note1: null,
        note2: null,
        note3: null,
        note4: null,
        note5: null
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.id)
      this.single = selection.length!==1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加投票信息";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const id = row.id || this.ids
      getVote(id).then(response => {
        this.form = response.data;
        this.open = true;
        this.title = "修改投票信息";
      });
    },

    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.id != null) {
            updateVote(this.form).then(response => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addVote(this.form).then(response => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 发布选项按钮操作 */
    addQuestion(row) {
      this.carryCurrentRowCode = row;
      this.$router.push({
        path: "/zonghe/drtpvoteoption",
        //query: this.carryCurrentRowCode
        query: {
          carryCurrentRowCode: this.carryCurrentRowCode
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const ids = row.id || this.ids;
      this.$modal.confirm('是否确认删除投票信息编号为"' + ids + '"的数据项？').then(function() {
        return delVote(ids);
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess("删除成功");
      }).catch(() => {});
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download('dr/vote/export', {
        ...this.queryParams
      }, `vote_${new Date().getTime()}.xlsx`)
    }
  }
};
</script>
